package com.central.apps.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.central.app.service.HandlerService;
import com.central.apps.dao.BlMessageDao;
import com.central.apps.entity.BlMessage;
import com.central.apps.service.MessageService;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.util.SysUserUtil;
import com.central.common.web.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 消息控制器
 *
 * @author JHY
 * @date 2022/10/31
 */
@Slf4j
@Api(tags = "消息通知")
@CrossOrigin
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private HandlerService handlerService;

    @Resource
    private BlMessageDao messageDao;

    @Resource
    private MessageService messageService;

    @PostMapping("/queryMessageList")
    public Map<String, Object> queryMessageList(@RequestBody Map<String, Object> map) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
        Long id = loginAppUser.getId();
        Map<String, Object> rsp = new HashMap<>(5);
        Integer page = MapUtil.getInt(map, "page");
        Integer limit = MapUtil.getInt(map, "limit");
        String isshow = MapUtil.getStr(map, "isshow");
        QueryWrapper<BlMessage> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("createtime");
        if (StringUtils.isNotBlank(isshow)) {
            wrapper.eq("isshow", isshow);
            wrapper.eq("recipient", id);
        }

        Page<BlMessage> pages = new Page<>(page, limit);
        IPage<BlMessage> iPage = messageDao.selectPage(pages, wrapper);
        long total = iPage.getTotal();
        rsp.put("count", total);
        rsp.put("code", 200);
        rsp.put("data", iPage.getRecords());
        return rsp;
    }

    @PostMapping("/queryMessage")
    public Map<String, Object> queryMessage(@RequestBody Map<String, Object> map) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
        Long id = loginAppUser.getId();
        Map<String, Object> rsp = new HashMap<>(5);
        Integer page = MapUtil.getInt(map, "page");
        Integer limit = MapUtil.getInt(map, "limit");

        String messagenum = MapUtil.getStr(map, "messagenum");
        String title = MapUtil.getStr(map, "title");
        QueryWrapper<BlMessage> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("isshow", "已读");
        queryWrapper.orderByDesc("createtime");
        queryWrapper.and(wrapper -> {
            wrapper.eq("createuser", id).or().eq("recipient", id);
        });
        if (StringUtils.isNotBlank(messagenum)) {
            queryWrapper.eq("messagenum", messagenum);
        }
        if (StringUtils.isNotBlank(title)) {
            queryWrapper.like("title", title);
        }
//        LambdaQueryWrapper<BlMessage> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(BlMessage::getIsshow, "已读").orderByDesc(BlMessage::getCreatetime);
//        wrapper.eq(BlMessage::getCreateuser, id).or().eq(BlMessage::getRecipient, id);
//        if (StringUtils.isNotBlank(messagenum)) {
//            wrapper.eq(BlMessage::getMessagenum, messagenum);
//        }
//        if (StringUtils.isNotBlank(title)) {
//            wrapper.like(BlMessage::getTitle, title);
//        }
        Page<BlMessage> pages = new Page<>(page, limit);
        IPage<BlMessage> iPage = messageDao.selectPage(pages, queryWrapper);
        rsp.put("code", 200);
        rsp.put("data", iPage);
        return rsp;
    }

    @PostMapping("/addMessage")
    public Result<?> addMessage(@RequestBody BlMessage message) {
        int res = messageService.addMessage(message);
        return Result.succeed(res, "添加成功");
    }

    @PostMapping("/updateMessage")
    public Result<?> updateMessage(@RequestBody BlMessage message) {
        int res = messageDao.updateById(message);
        return Result.succeed(res, "修改成功");
    }

}
